
class Widget extends MovieClip
{
	public var title:TextField;
	public var clock:TextField;
	
	public var canvas:MovieClip;
	public var refresh:MovieClip;
	
	public var offset:Number;
	
	public var callers:Object;
	public var userClips:Array;
	
	public var frame:Number;
	
	public function reload ()
	{
		if (this.userClips != undefined)
		{
			for (var i = 0; i < this.userClips.length; i++)
				this.userClips[i].removeMovieClip ();
		}
		
		var me = this;
		me.callers = new Array ();
		
		var xml:XML = new XML ();
		xml.ignoreWhite = true;

		xml.onLoad = function (success:Boolean)
		{
			trace ("load = " + success);
			
			var channel:XMLNode = this.firstChild.firstChild;

			for (var i = 0; i < channel.childNodes.length - 1; i++)
			{
				var node:XMLNode = channel.childNodes[i];
				
				if (node.nodeName == "title")
					me.title.text = node.firstChild.nodeValue;
				else if (node.nodeName == "item")
				{
					var caller = new Object ();
					
					for (var j = 0; j < node.childNodes.length; j++)
					{
						var childNode = node.childNodes[j];

						if (childNode.nodeName == "title")
						{
							var title = childNode.firstChild.nodeValue;
							title = title.split ("\n").join (" ");
							title = title.split ("\r").join (" ");

							while (title.indexOf ("  ") != -1)
								title = title.split ("  ").join (" ");
						
							var components:Array = title.split ("(");
							
							caller["name"] = components[0];
							caller["phone"] = components[1].split (")")[0];

							if (caller["phone"].length == 10)
							{
								var phone = caller["phone"];
								
								caller["phone"] = "(" + phone.slice (0, 3) + ") " + 
													phone.slice (3, 6) + "-" + 
													phone.slice (6, 10);
							}
						}
						else if (childNode.nodeName == "guid") 
						{
							var dateString = childNode.firstChild.nodeValue;
							
							var callDate = new Date ();
							
							callDate.setFullYear (dateString.slice (0, 4));
							callDate.setMonth (dateString.slice (5, 7));
							callDate.setDate (dateString.slice (8, 10));
							
							callDate.setHours (dateString.slice (11, 13));
							callDate.setMinutes (dateString.slice (14, 16));
							callDate.setSeconds (dateString.slice (17, 19));
							
							caller["date"] = callDate; 
						}
						// else
						//	trace ("node = " + childNode.nodeName);
						
					}
					
					me.callers.push (caller);
				}
			}
			
			me.display ();
		}
		
		xml.load ("http://foo:bar@www.aetherial.net/callers/cgi-bin/interface.cgi?do=rss");

	}
	
	public function display ()
	{
		this.userClips = new Array ();

		var now:Date = new Date ();

		var i:Number = 0;

		for (var i = 0; i < this.callers.length; i++)
		{
			var caller:Object = this.callers[i];
			
			var clip:MovieClip = this.canvas.attachMovie ("Caller", "caller" + i, 
															this.canvas.getNextHighestDepth ());
			this.userClips.push (clip);
			
			clip.name.text = caller["name"];
			clip.phone.text = caller["phone"];

			var d:Date = caller["date"];
			
			var minutes = d.getMinutes ();
			
			if (("" + minutes).length == 1)
				minutes = "0" + minutes;

			var month = "";
			
			switch (now.getMonth ())
			{
				case 0: month = "Jan. "; break;
				case 1: month = "Feb. "; break;
				case 2: month = "Mar. "; break;
				case 3: month = "Apr. "; break;
				case 4: month = "May "; break;
				case 5: month = "June "; break;
				case 6: month = "July "; break;
				case 7: month = "Aug. "; break;
				case 8: month = "Sep. "; break;
				case 9: month = "Oct. "; break;
				case 10: month = "Nov. "; break;
				case 11: month = "Dec. "; break;
			}

			clip.date.text = d.getHours () + ":" + minutes + " " + month + " " + 
							 d.getDate () + ", " + d.getFullYear ();
			
				
			clip._y = 5 + (i * 49);
			clip._x = 148;
		}
		
		this.onMouseDown = function ()
		{
			this.mouseStart = this._ymouse;
		}
		
		this.onMouseUp = function ()
		{
			this.mouseStart = undefined;
		}
		
		this.onMouseMove = function ()
		{
			if (this.mouseStart != undefined)
			{
				var currentMouse:Number = this._ymouse;
				
				var newY:Number = this.canvas._y - (this.mouseStart - currentMouse);
			
				if (newY + canvas._height >= 194 && newY <= 26)
					this.canvas._y = newY;
				
				this.mouseStart = currentMouse;
			}
		}
		
		var me = this;
		
		this.refresh.onPress = function ()
		{
			me.reload ();
		}
	}
	
	public function onEnterFrame ()
	{
		this.reload ();
		
		this.frame = 1;
		
		this.onEnterFrame = function ()
		{
			if (this.frame % 12 == 0)
			{
				var now:Date = new Date ();
				
				var hours:Number = now.getHours ();
				var minutes:Number = now.getMinutes ();
				
				var dateString:String = (hours % 12) + ":";
				
				if ((hours % 12) == 0)
					dateString = "12:";
				if (minutes < 10)
					dateString += "0"
					
				dateString += minutes + " ";
				
				if (hours >= 12)
					dateString += "PM";
				else
					dateString += "AM";
					
				if (this.time.text != dateString)
					this.time.text = dateString;

				switch (now.getMonth ())
				{
					case 0: dateString = "Jan. "; break;
					case 1: dateString = "Feb. "; break;
					case 2: dateString = "Mar. "; break;
					case 3: dateString = "Apr. "; break;
					case 4: dateString = "May "; break;
					case 5: dateString = "June "; break;
					case 6: dateString = "July "; break;
					case 7: dateString = "Aug. "; break;
					case 8: dateString = "Sep. "; break;
					case 9: dateString = "Oct. "; break;
					case 10: dateString = "Nov. "; break;
					case 11: dateString = "Dec. "; break;
				}
				
				dateString += (now.getDate () + 1) + ", ";
				dateString += " " + now.getFullYear ();

				if (this.date.text != dateString)
					this.date.text = dateString;
				

				this.frame = 0;
			}
		
			this.frame += 1;
		}
	}
}

